#include "PreCompiled.h"
#include "Action.h"
#include "Command.h"
Action::Action(Command* pcCmd,QObject* parent )
:QObject(parent),_action(new QAction(this)),_pcCmd(pcCmd)
{
	_action->setObjectName(QString::fromAscii(_pcCmd->getName()));
	connect(_action,SIGNAL(triggered(bool)),this,SLOT(onActivated()));
}
Action::Action(Command* pcCmd,QAction* action,QObject* parent)
:QObject(parent),_action(action),_pcCmd(pcCmd)
{
   _action->setParent(this);
   _action->setObjectName(QString::fromAscii(_pcCmd->getName()));
   connect(_action,SIGNAL(triggered(bool)),this,SLOT(onActivated()));
}
Action::~Action(void)
{
	delete _action;
}
void Action::addTo(QWidget* w)
{
  w->addAction(_action);
}

void Action::onActivated()
{
	_pcCmd->invoke(0);
}
void Action::setEnabled(bool b)
{
   _action->setEnabled(b);
}
void Action::setVisible(bool b)
{
   _action->setVisible(b);
}

void Action::setCheckble(bool b)
{
   _action->setCheckable(b);
}
void Action::setChecked(bool b)
{
   _action->setCheckable(b);
}
bool Action::isChecked()const
{
   return _action->isChecked();
}

void Action::setShortcut(const QString& s)
{
    _action->setShortcut(s);
}
QKeySequence Action::shortcut()const
{
   return _action->shortcut();
}
void Action::setIcon(const QIcon& icon)
{
   _action->setIcon(icon);
}
void Action::setStausTip(const QString& s)
{
  _action->setStatusTip(s);
}
QString Action::stausTip()const
{
  return _action->statusTip();
}
void Action::setText(const QString& s)
{ 
   _action->setText(s);
}
QString Action::text()const
{
    return _action->text();
}

void Action::setToolTip(const QString& s)
{
    _action->setToolTip(s);
}
QString Action::toolTip()const
{
    return _action->toolTip();
}

void Action::setWhatThis(const QString& s)
{
  _action->setWhatsThis(s);
}
QString Action::whatThis()const
{
  return _action->whatsThis();
}

void Action::OnToggled(bool b)
{
	_pcCmd->invoke(b? 1:0);
}

// ---------------------------------------------- [4/28/2013 Administrator]

ActionGroup::ActionGroup(Command* pcCmd,QObject* parent)
:Action(pcCmd,parent),_group(0),_dropDown(false)
{
  _group = new QActionGroup(this);
  connect(_group,SIGNAL(triggered(QAction*)),this,SLOT(onActivated(QAction*)));
}
ActionGroup::~ActionGroup()
{
  delete _group;
}

void ActionGroup::addTo(QWidget* w)
{
  if(_dropDown)
  {
	  if(w->inherits("QMenu"))
	  {
		  QMenu* menu = qobject_cast<QMenu*>(w);
		  menu = menu->addMenu(_action->text());
          menu->addActions(_group->actions());
	  }else if(w->inherits("QToolBar"))
	  {

	  }else
	  {
		  w->addActions(_group->actions());
	  }

  }else
  {
	  w->addActions(_group->actions());
  }
}
void ActionGroup::setEnabled(bool b)
{
	Action::setEnabled(b);
	_group->setEnabled(b);
}
void ActionGroup::setDisabled(bool b)
{
	Action::setEnabled(!b);
	_group->setDisabled(b);
}
void ActionGroup::setExclusive(bool b)
{
    _group->setExclusive(b);
}
void ActionGroup::setVisible(bool b)
{
	Action::setVisible(b);
	_group->setVisible(b);
}

QAction* ActionGroup::addAction(QAction* action)
{
    int index = _group->actions().size();
    action = _group->addAction(action);
    action->setData(QVariant(index));
	return action;

}
QAction* ActionGroup::addAction(const QString& text)
{
    int index = _group->actions().size();
	QAction* action = _group->addAction(text);
	action->setData(QVariant(index));
	return action;
}
QList<QAction*>ActionGroup::actions()const
{
    return _group->actions();
}
int ActionGroup::checkedAction()const
{
     QAction* checkaction = _group->checkedAction();
	 return checkaction ? checkaction->data().toInt(): -1;

}
void ActionGroup::setCheckedAction(int i)
{
   _group->actions()[i]->setChecked(true);
}
void ActionGroup::onActivated()
{
    _pcCmd->invoke(this->property("defaultAction").toInt());
}
void ActionGroup::onActivated(int index)
{
    _pcCmd->invoke(index);
}
void ActionGroup::onActivated(QAction* a)
{
	int index =  _group->actions().indexOf(a);

	QList<QWidget*> widgets = a->associatedWidgets();
	for(QList<QWidget*>::Iterator it = widgets.begin(); it != widgets.end(); it++)
	{
		QMenu* menu = qobject_cast<QMenu*>(*it);
		if(menu)
		{
			QToolButton* toolbutton = qobject_cast<QToolButton*>(menu->parent());
			if(toolbutton)
			{
                toolbutton->setIcon(a->icon());
				this->setProperty("defaultAction",QVariant(index));
			}
		}
	}
    _pcCmd->invoke(index);
}


#include "moc_action.cpp"

